package com.liqi.controller;

import com.liqi.beans.*;
import com.liqi.service.OrderService;
import com.netflix.client.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {





    @Autowired
    private OrderService orderService;

    @PostMapping("login")
    public Result login(@RequestBody User user){

        User login = orderService.login(user);

        if(login == null){
            return new Result(false, StatusCode.LOGINERROR,"账户或密码不对");
        }




        String token = orderService.createToken(login);

        return new Result(true, StatusCode.OK,"登录成功",token);


    }

    @GetMapping
    public Map<String,Object> getOrders(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        Integer userid = Integer.valueOf(request.getHeader("userid"));
        String email = orderService.findById(userid);
        List<Course> courses = orderService.getCourses();

        map.put("email",email);
        map.put("courses",courses);
        return map;
    }

    @PostMapping("save")
    public Result save(@RequestBody Order order, HttpServletRequest request){

        Integer userid = Integer.parseInt( request.getHeader("userid"));

        List<Course> courses = order.getCourses();

        order.setUserId(userid);
        orderService.add(order);

        return new Result(true, StatusCode.OK,"保存成功");
    }

}
